package com.yanqu.road.server.cmd.activity.busyfarming;


import com.yanqu.road.entity.activity.busyfarming.BusyFarmingHelpRecord;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingPb;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_BUSY_FARMING_FRIEND_HELP_LIST,desc = "亲友来助-列表")
public class BusyFarmingHelpRecordListCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }

        BusyFarmingProto.BusyFarmingHelpRecordRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingHelpRecordRespMsg.newBuilder();

        List<BusyFarmingHelpRecord> list = BusyFarmingMgr.getUserHelpRecord(player.getUserId());
        if(list != null){
            int size = list.size();
            int num = 0;
            for(int i = size - 1; i >= 0; i--){
                BusyFarmingHelpRecord record = list.get(i);
                record.setReaded(true);
                BusyFarmingProto.BusyFarmingHelpRecordTemp.Builder builder = BusyFarmingPb.getHelpRecordBuilder(record);
                respMsg.addRecord(builder);
                num++;
                if(num >= BusyFarmingMgr.RETAIN_RECORD_DATA_NUM){
                    break;
                }
            }
        }
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_BUSY_FARMING_FRIEND_HELP_LIST, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.BusyFarmingHelpRecordRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingHelpRecordRespMsg.newBuilder();
            respMsg.setRet(0);
            player.sendPacket(Protocol.U_BUSY_FARMING_FRIEND_HELP_LIST, respMsg);
        }
    }
}
